package server.world.action

import server.world.character._
import server.world.map._
import server.world.item._

class Move(val char:GameCharacter,targetTile:Tile) extends Action{
  override val skillname = "BASIC"
  var ap = char.moveAP 
  def predicate() = {
                    targetTile.map.contains(targetTile) & 
                    targetTile.texure.ispassable() &
                    targetTile.isAdjacent(char.tile)& 
                    targetTile.isEmpty
                    }
  
  def exe() = {
    char.tile.characterOnTile = null
    char.tile = targetTile 
    char.tile.characterOnTile = char
    System.out.println("   *Character " + char.toString() + " moved to tile (" + char.tile.xCor.toString() + "," + char.tile.yCor.toString() + ")")
  }
}

class Attack(val char:GameCharacter, target:GameCharacter)extends Action {
        override val skillname = "BASIC"
        var ap = 0f
        var logdmg:Float = 0f
        def predicate() = {
          target.tile.isAdjacent(char.tile) & target.isAlive
        }
        
        def exe() = {
            if(char.tapR(char.getAcc)>char.tapR(target.getEV)){
            val dmg = char.getDamage - char.tapR(target.getAC)
            target.reciveDMG(dmg) 
            logdmg = dmg
            }
            char.attackdisabled = char.attackAP()
        }
        System.out.println("   *Character " + char.toString() + " attacked " + target.toString + " for " + logdmg + " HP")
}
        
class Fire(val char:GameCharacter, target:GameCharacter) extends Action{
  override val skillname = "BASIC"
  var ap = char.attackAP
  def predicate() = {
              char.isabletoFire & 
              char.visibleTiles.contains(target.tile) &  
              target.isAlive
  }
  
  def exe() = {
                  if(char.tapR(char.getAcc)>char.tapR(target.getEV)){
                  target.reciveDMG(char.getDamage - char.tapR(target.getAC))
                }
  }
}

class EquipItem(val char:GameCharacter, item:Item) extends Action{
 override val skillname = "BASIC"
 var ap = 2f
 
 def predicate() = char.backpack.contains(item)
 def exe() = {
   char.inv = item match {
     case item:TwoHanded => char.inv.copy(Hand1 = Some(item.asInstanceOf[Weapon]), Hand2 = None)
     case item:OffHand => item match{
                       case item:OffHand if(char.inv.Hand1.getOrElse(false).isInstanceOf[TwoHanded])=>  item match{
                                                                                                      case _:Weapon => char.inv.copy(Hand1 = Some(item.asInstanceOf[Weapon]))
                                                                                                      case _Armour => char.inv.copy(Hand1 = None, Hand2 = Some(item.asInstanceOf[OffHand]))
                                                                                                     }
                       case item:Weapon if(char.inv.Hand2.isEmpty) => char.inv.copy(Hand2 = Some(item.asInstanceOf[OffHand]))
                       case item:Weapon if(!char.inv.Hand1.isEmpty & !char.inv.Hand2.isEmpty) => char.inv.copy(Hand1 = Some(item.asInstanceOf[Weapon]))
                       case item:Armour  => char.inv.copy(Hand2 = Some(item.asInstanceOf[OffHand]))
                       }
     case item:BArmour => char.inv.copy(Armour = Some(item.asInstanceOf[BArmour]))
     case item:HeadGear => char.inv.copy(HeadGear = Some(item.asInstanceOf[HeadGear]))
     case item:Boots => char.inv.copy(Boots =  Some(item.asInstanceOf[Boots]))
     case item:Amulet => char.inv.copy(Neck = Some(item.asInstanceOf[Amulet]))
     case item:Ring => if(char.inv.Ring1.isEmpty | (!char.inv.Ring1.isEmpty & !char.inv.Ring2.isEmpty) )  char.inv.copy(Ring1 = Some(item.asInstanceOf[Ring]))
                       else char.inv.copy(Ring2 = Some(item.asInstanceOf[Ring]))
     case item:Quiver => char.inv.copy(Quiver = Some(item.asInstanceOf[Quiver]))
   }
 }
}

class EmptySlot(val char:GameCharacter, slot:Option[Item]) extends Action{
 override val skillname = "BASIC"
 var ap = 1f
 
 def predicate() = true
 
 def exe(){
   char.inv match  {
     case _:Inventory if (char.inv.Hand1 == slot) => char.inv = char.inv.copy(Hand1 =None)
     case _:Inventory if (char.inv.Hand2 == slot) => char.inv = char.inv.copy(Hand2 =None)
     case _:Inventory if (char.inv.Armour == slot) => char.inv = char.inv.copy(Armour =None)
     case _:Inventory if (char.inv.HeadGear == slot) => char.inv = char.inv.copy(HeadGear =None)
     case _:Inventory if (char.inv.Boots == slot) => char.inv = char.inv.copy(Boots =None)
     case _:Inventory if (char.inv.Neck == slot) => char.inv = char.inv.copy(Neck =None)
     case _:Inventory if (char.inv.Ring1 == slot) => char.inv = char.inv.copy(Ring1 =None)
     case _:Inventory if (char.inv.Ring2 == slot) => char.inv = char.inv.copy(Ring2 =None)
     case _:Inventory if (char.inv.Quiver == slot) => char.inv = char.inv.copy(Quiver =None)
   }
 }
 
}
